//index.js  
//获取应用实例  
var app = getApp();
var common = require("../../utils/common.js");
var that
Page({
  data: {
    s_position:0,  //当前滚轴位置
    map_hidden:true,
    fixed_hidden: false, 
    // tab切换  wx.getUserInfo
    currentTab: 0,
    orderList0: [1,1,1,1,1,1],
    orderList1: [],
    h: 0,
    house_type:"",
    h_phone:"",
    h_business_hours:"",
    h_address:"",
    banner: [], //链接图及关联信息
    service:"",  //服务项目
    ser_id:"", //从申请会员传回的服务id
    shop_point:"", //店铺经纬度
    shopIndex:[],
    objectShop: [],
    shop_id:''   
  },
  onLoad: function (options) {    
    
    if (!wx.getStorageSync('shop_id')) {     
      return false;
    }

    
    that=this;
    
    that.get_hotel_msg();//获取酒店信息
    that.get_service_msg();//获取服务项目
    that.get_banner_msg();//获取链接图信息
    that.get_shop_msg(); //获取店铺信息
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          h: res.windowHeight,
          
        });
      }
    });

    this.setData({
      index: wx.getStorageSync('shop_index'),
      shop_id:wx.getStorageSync('shop_id')
    });

    
    
    

    this.setData({ currentTab: 0,});
    that.get_person_f_rr();//获取个人在人人商城的信息
  },
  onShow: function () {
   
    if (app.data.to_ser==1) { //从看猫界面给猫加服务
      app.data.to_ser = 0;
      that.setData({s_position:that.data.h*0.5-3})
      var e = { "target": { "dataset": { "current":1} } }
      that.swichNav(e);
      }
    if (app.data.iswarm == 1) {//是否弹出过提醒注册会员的界面
      app.data.iswarm = 2
      var e = { "currentTarget": { "dataset": {"msg":{"id":that.data.ser_id}}}}
      that.adv_order(e)
    }
  },
 
 bindPickerChange: function(e) {
    that=this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var shop_id = that.data.shopIndex[e.detail.value].id ;
    wx.setStorageSync('shop_id',shop_id);
    this.setData({
      index: e.detail.value
    });
   wx.setStorageSync('shop_index', e.detail.value );

    wx.reLaunch({
      url: 'home'
    })   
  },
 
  //改变选项卡
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current});
    this.setData({ map_hidden: true });  //改变选项卡时地图隐藏
  },
  //选项卡标题绑定的事件
  swichNav: function (e) {
    var that = this;
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      var current = e.target.dataset.current;
      that.setData({
        currentTab: parseInt(current),
      });
    };
  },
  select_caredate_limit:function(){//点击选择托养日期
    // wx.navigateTo({
    //   url: './caredate'
    // });
    wx.navigateTo({ 
      url: '../my_care/adv_cares'
    });
  },
  house_detail:function(e){  //点击预约按钮查看房间详情
    wx.navigateTo({
      url: './house_detail?house_id=' + e.currentTarget.dataset.id
    });
  },
  adv_btn: function (e) {//点击预约按钮查看房间详情并选择日期
    wx.navigateTo({
      url: './house_detail?house_id=' + e.currentTarget.dataset.id+'&adv=true'
    });
  },
  adv_order:function(e){  //点击下单跳到服务项目
    if (app.data.person_msg_rr.level < 1 && app.data.iswarm==0) {
      wx.navigateTo({ url: '../member/application_member?f=home&ser_id=' + e.currentTarget.dataset.msg.id })
      return;
    }//若非会员则提示注册会员
    
    wx.navigateTo({
      url: '../selected_server/selected_server?ser_id='+e.currentTarget.dataset.msg.id
    });
  },
  get_hotel_msg: function () {  //获取酒店所有信息 
    var shop_id = wx.getStorageSync('shop_id');
    if(shop_id=='') {
      return false;
    }
    app.req("get_room_type.html", { sessionid: wx.getStorageSync('3rd_session'),"shop_id":shop_id }, function (res) {
      if (res.status == "success") {
        app.hotelData.house_type = res.mess;  //酒店类型
        app.hotelData.house_notict = res.hotel;  //酒店入住须知
        // console.log(res);
        that.setData({ house_type: res.mess, imgUrls: res.hotel.booking_info.split(',')});
        that.setData({ h_phone: res.hotel.phone, h_business_hours: res.hotel.business_hours, h_address: res.hotel.address.split("(")[0] });
      } else if (res.status == "error") {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_hotel_msg()
          }
        })
      }
    })
  },
  get_service_msg: function () {  //获取酒店所有信息
    
    app.req("get_service.html", {"shop_id":wx.getStorageSync('shop_id')}, function (res) {
      // console.log(res)
      if (res.status == "success") {
        for (var i = 0; i < res.mess.length;i++){
          if (res.mess[i].time!=0){
            res.mess[i].time = "-" + res.mess[i].time+"分钟"
          } else { res.mess[i].time = ""}
          if (res.mess[i].type == 1){
            res.mess[i].type = ''
          } else { res.mess[i].type = "不对非托养猫开放" }
          // res.mess[i].unvip_price = Math.ceil(parseFloat(res.mess[i].price)*3/2)
        }
        that.setData({service:res.mess});
      } else if (res.status == "error") {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_service_msg()
          }
        })
      }
    })
  },
  get_banner_msg:function(){   
    app.req("get_banner.html", {"shop_id":wx.getStorageSync('shop_id')}, function (res) {
      // console.log(res)
      if (res.status == "success") {
        that.setData({ banner: res.mess });
      } else if (res.status == "error") {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_service_msg()
          }
        })
      }
    })
  },
 
  get_shop_msg: function () {
  
    app.req("get_shop.html", {}, function (res) {
      var shop_arr = [];
      if (res.status == "success") {
        for (var shop in res.mess) {
          shop_arr.push(res.mess[shop]);
          
        }

        that.setData({ shopIndex:shop_arr });
        that.setData({ objectShop: res.mess });
      } else if (res.status == "error") {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_service_msg()
          }
        })
      }
    })
  },
  cellphone: function () {
    wx.makePhoneCall({
      phoneNumber: app.hotelData.house_notict.phone //真实的电话号码
    })
  },
  s_in: function (e) {//滚动中地图隐藏
    // console.info(e.detail.scrollTop)
    if (e.detail.scrollTop>(that.data.h*0.5-1)){
      that.setData({ fixed_hidden: true });
    }else{
      that.setData({ fixed_hidden: false });
    }
    /*if(e.timeStamp-this.data.position>0)//下滑
    {
      this.setData({ map_hidden: false });
    }else{//上滑动
      this.setData({ map_hidden: true });
    }
    this.setData({ position: e.timeStamp });*/
  },
  call_phone:function(){
    wx.makePhoneCall({
      phoneNumber:that.data.h_phone ,//拨打电话号码 电话号码
      success:function(res){
      }
    })
  },
  map_adr:function(){//点击地图跳到大地图界面
    wx.navigateTo({
      url: './map_adr',
    })
  },
 
  get_person_f_rr:function(){
      app.getUserInfo(function (res) {//重新请求获取各人在商城的会员信息
        // that.clear_overtime_order(); //删除未付款的过时订单
        var openid =   wx.getStorageSync('openid');
      app.req("get_person_f_rr.html", { "openid": openid }, function (res) {
        if (res.status == "success") { 
          app.data.person_msg_rr=res.mess
        } else if (res.status == "error") {
          app.toast(res.mess)
        } else {
          app.modal("错误提示", "网络错误，是否重试？", function (re) {
            if (re) {
              that.get_person_f_rr()
            }
          })
        }
        })
      });
  },
  img_lianjie:function(e){
    var i = e.currentTarget.dataset.idx;
    switch(that.data.banner[i].type){
      case 0: //只显示图片
        
        break;
      case 1: //跳转房间详情
        wx.navigateTo({
          url: './house_detail?house_id=' + that.data.banner[i].value
        })
      case 2://跳转商品详情
        wx.navigateTo({
          url: '../shop/goods_detail?productId=' + that.data.banner[i].value
        });
      break;
      case 3://跳转外部
        wx.navigateTo({
          url: './to_web?path=' + that.data.banner[i].value
        });
      break;
    }
    
  }


})